package com.one.group.admin.controller;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.processor.util.StrUtil;
import com.one.group.enums.ErrorCode;
import com.one.group.model.dto.*;
import com.one.group.model.entity.Contract;
import com.one.group.model.entity.ParkingSpace;
import com.one.group.model.entity.Vendor;
import com.one.group.model.vo.ParkingContractListVo;
import com.one.group.result.Result;
import com.one.group.service.ContractService;
import com.one.group.service.ParkingSpaceService;
import com.one.group.utils.ResultUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;
import java.util.List;


@RestController
@RequestMapping("contract")
@Tag(name = "合同管理接口")
public class ContractController {
    @Autowired
    ContractService contractService;
    @Autowired
    ParkingSpaceService parkingSpaceService;
    @GetMapping("getPs")
    @Operation(summary = "查询小区车位信息")
    public Result getParkingSpace(@RequestParam Long cellId){
        QueryWrapper queryWrapper = QueryWrapper.create();
        queryWrapper.from(ParkingSpace.class);
        queryWrapper.eq(ParkingSpace::getCellId,cellId);
        List<ParkingSpace> list = parkingSpaceService.list(queryWrapper);
        return ResultUtil.success(list);
    }

    @PostMapping("list")
    @Operation(summary = "查询车位合同列表")
    public Result list(@RequestBody ContractQueryDto contractQueryDto){
        Long tenantId = Convert.toLong(StpUtil.getExtra("tenant_id"));
        contractQueryDto.setTenantId(tenantId);
        PageHelper.startPage(contractQueryDto.getPage(), contractQueryDto.getLimit());
        List<ParkingContractListVo> ParkingContractListVos = contractService.findContractByQueryDto(contractQueryDto);
        PageInfo<ParkingContractListVo> pageInfo = new PageInfo<>(ParkingContractListVos);
        return ResultUtil.success(pageInfo);
    }


    @GetMapping("/find")
    @Operation(summary = "根据id查询合同信息")
    public Result find(@RequestParam Long id){
        return ResultUtil.success(contractService.findContractById(id));
    }
    @PostMapping("/findCharges")
    @Operation(summary = "查询租金信息")
    public Result findCharges(@RequestBody ChargeStandardsFindDto chargeStandardsFindDto){
        Long tenantId = Convert.toLong(StpUtil.getExtra("tenant_id"));
        chargeStandardsFindDto.setTenantId(tenantId);
        return ResultUtil.success(contractService.findChargeStandardsByDto(chargeStandardsFindDto));
    }
//    @GetMapping("findAll")
//    @Operation(summary = "查询所有合同信息")
//    public Result findAll() {
//        return ResultUtil.success(contractService.list());
//    }
    @GetMapping("/delete")
    @Operation(summary = "删除合同信息")
    public Result delete(@RequestParam Long id){
        if (contractService.DeleteContractById(id))
            return ResultUtil.success();
        else return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }
    @PostMapping("/add")
    @Operation(summary = "添加合同信息")
    public Result add(@RequestBody ContractInsertDto contractInsertDto){
        if (contractService.InsertContractByDto(contractInsertDto)) return ResultUtil.success();
        else return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }
    @PostMapping("/update")
    @Operation(summary = "修改合同信息")
    public Result update(@RequestBody ContractInsertDto contractInsertDto){
        if (contractService.updateContractByDto(contractInsertDto)) return ResultUtil.success();
        else return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }
    @PostMapping("/updateOne")
    @Operation(summary = "只修改合同信息")
    public Result updateOneByDto(@RequestBody ContractOneUpdateDto contractOneUpdateDto){
        Contract contract = new Contract();
        BeanUtils.copyProperties(contractOneUpdateDto,contract);
        if (contractService.updateById(contract)) return ResultUtil.success();
        else return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }
    // 应该使用 Contract 相关的查询
    @PostMapping("oneList")
    @Operation(summary = "查询合同列表")
    public Result Onelist(@RequestBody ContractQueryDto contractQueryDto) {
//        1.准备分页条件
        Page<Contract> page = new Page<>(contractQueryDto.getPage(), contractQueryDto.getLimit());
//        2.链式编程准备查询条件
        QueryWrapper queryWrapper = new QueryWrapper();

// 2. 构建查询条件
        String name = contractQueryDto.getQuery();
        if (!StrUtil.isBlank(name)) {
            queryWrapper.where(
                    QueryMethods.column(Contract::getTenantContract).like(name)
                            .or(QueryMethods.column(Contract::getTenantPhone).like(name))
            );
        }
//        3.查询，返回分页数据
        Page<Contract> contractPage = contractService.page(page, queryWrapper);
        return ResultUtil.success(contractPage);
    }
    @PostMapping("renewal")
    @Operation(summary = "续租")
    public Result renewal(@RequestBody RenewalDto renewalDto){
        Contract contract = new Contract();
        BeanUtils.copyProperties(renewalDto,contract);
        Contract contract1 = contractService.getById(renewalDto.getId());
        if (contract1.getContractStatus()!=0){
            contract.setContractStatus(0);
        }
        if (contractService.updateById(contract)) return ResultUtil.success();
        else return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }
    @GetMapping("termination")
    @Operation(summary = "退租")
    public Result termination(@RequestParam Long id){
        Contract contract = new Contract();
        contract.setId(id);
        contract.setContractStatus(2);
        if (contractService.updateById(contract)) return ResultUtil.success();
        else return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }
//    @PostMapping("/deleteBatch")
//    @Operation(summary = "批量删除合同信息")
//    public Result deleteBatch(@RequestBody BaseDeleteDto baseDeleteDto){
//        if (contractService.removeByIds(baseDeleteDto.getIds())) return ResultUtil.success();
//        else return ResultUtil.error(ErrorCode.OPERATION_ERROR);
//    }

}
